package org.darkforest.common.services.impl;

import org.apache.commons.lang.StringUtils;
import org.darkforest.common.services.GiftCustomizationService;
import org.darkforest.common.services.SimpleRedisService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.common.util.JSONUtils;
import org.darkforest.model.GiftCardTemplate;
import org.darkforest.model.GiftDistribution;
import org.darkforest.model.GiftPackage;
import org.darkforest.vo.GiftCustomizationVo;
import org.darkforest.vo.MemberBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/4/21.
 */
@Service
public class GiftCustomizationServiceImpl implements GiftCustomizationService {

    @Autowired
    private SimpleRedisService simpleRedisService;

    /**
     * 获取用户自定义包装以及配送内容以及总费用
     *
     * @return
     * @param userId
     */
    @Override
    public GiftCustomizationVo getGiftCustomization(Integer userId) {


        //配送时间
        String postTime = simpleRedisService.getByKey(userId + "_post_time");
        if(StringUtils.isBlank(postTime)){

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c1 = Calendar.getInstance();
            c1.setTime(new Date());
            c1.add(Calendar.DATE, 2);
            postTime = sdf.format(c1.getTime());


//            postTime =DateUtil.addDay(DateUtil.dateToString(new Date(), "yyyy-MM-dd'T'HH:mm"), 2);
            simpleRedisService.setValueInTime(userId + "_post_time", postTime, 7200L);
        }

        GiftCustomizationVo customizationVo = new GiftCustomizationVo();
        //包装
        String packages =  simpleRedisService.getByKey(userId+"_package");
        //卡片
        String card =  simpleRedisService.getByKey(userId+"_card");
//        快递定制
        String post =  simpleRedisService.getByKey(userId + "_post");

        String fId = simpleRedisService.getByKey(userId + "_fId");

        GiftPackage giftPackage = JSONUtils.toBean(packages, GiftPackage.class);

        GiftCardTemplate giftCardTemplate = JSONUtils.toBean(card, GiftCardTemplate.class);

        List<GiftDistribution> giftDistributionList = JSONUtils.toList(JSONUtils.toJSONArray(post), GiftDistribution.class);

        customizationVo.setPostTime(postTime);
        customizationVo.setGiftPackageJson(packages);
        customizationVo.setGiftCardTemplateJson(card);
        customizationVo.setGiftDistributionListJson(post);
        customizationVo.setGiftPackage(giftPackage);
        customizationVo.setGiftCardTemplate(giftCardTemplate);
        customizationVo.setGiftDistributionList(giftDistributionList);
        customizationVo.setfId(StringUtils.isBlank(fId)?null:fId);
        return customizationVo;
    }

    @Override
    public void deleteGiftCustomization(MemberBase member) {
        simpleRedisService.deleteByKey(member.getUserId() + "_cartids");
        //包装
        simpleRedisService.deleteByKey(member.getUserId() + "_package");
        //卡片
        simpleRedisService.deleteByKey(member.getUserId() + "_card");
//        快递定制
        simpleRedisService.deleteByKey(member.getUserId() + "_post");
        //配送时间
        simpleRedisService.deleteByKey(member.getUserId() + "_post_time");
        //关联的节日提醒
        simpleRedisService.deleteByKey(member.getUserId() + "_fId");

    }
}
